package tt.dz.dao.ext.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.map.ListOrderedMap;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Repository;

import tt.dz.dao.base.OmsCommonDao;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.entity.system.OmsOperList;
import tt.dz.util.Constants;

/**
 * 管理员数据操作dao
 * @author huanghao
 * @modify pengyu,huanghao
 * @date 2015年12月16日
 * @update date  2015年1月18日
 */
@Repository
public class OmsOperListDao extends OmsCommonDao<OmsOperList>
{
	public static final class Sql
	{
		public static  String FIND_USER_BY_OPER_MERCHANT_TABLE;
		public static final int OPER_MERCHANT_TABLE_PNUM = 4;
		public static  String FIND_USER_BY_FACTORY_TABLE;
		public static final int FACTORY_TABLE_PNUM = 2;
		
		public static  String FIND_USER_BY_OM_BRANCH_TABLE;
		public static final int OM_BRANCH_TABLE_PNUM = 2;
		static 
		{
			StringBuilder sb = new StringBuilder();
			sb.append("( ");
			sb.append(" select * from oms_oper_list oper where oper.entity_type = 1 and oper.entity_id = ? ");
			sb.append(" union all ");
			sb.append(" select * from oms_oper_list oper where oper.entity_type = 2 and oper.entity_id in  ( ");
			sb.append(" select oper_merchant_branch_id from oms_operation_branch_sp_list ob where ob.oper_merchant_id = ? and ob.valid_flag = 1 )");
			sb.append(" union all ");
			sb.append(" select * from oms_oper_list oper where oper.entity_type = 4 and oper.entity_id in ( ");
			sb.append(" select merchant_id from oms_normal_sp_list ob where ob.entity_type = 1 and ob.entity_id = ? and ob.valid_flag = 1 )");
			sb.append(" union all ");
			sb.append(" select * from oms_oper_list oper where oper.entity_type = 4 and oper.entity_id in  ( ");
			sb.append(" select merchant_id from oms_normal_sp_list ob where ob.entity_type = 2 and ob.valid_flag = 1 and ob.entity_id in ( ");
			sb.append(" select oper_merchant_branch_id from oms_operation_branch_sp_list ob where ob.oper_merchant_id = ? and ob.valid_flag = 1)");
			sb.append(" ) ) tbl");
			FIND_USER_BY_OPER_MERCHANT_TABLE = sb.toString();
			sb.setLength(0);
			sb.append("( ");
			sb.append(" select * from oms_oper_list oper where oper.entity_type = 3 and oper.entity_id = ? ");
			sb.append(" union all ");
			sb.append(" select * from oms_oper_list oper where oper.entity_type = 4 and oper.entity_id in ( ");
			sb.append(" select merchant_id from oms_normal_sp_list ob where ob.entity_type = 3 and ob.entity_id = ?  and ob.valid_flag = 1 )");
			sb.append(" )  tbl");
			FIND_USER_BY_FACTORY_TABLE = sb.toString();
			sb.setLength(0);
			sb.append("( ");
			sb.append(" select * from oms_oper_list oper where oper.entity_type = 2 and oper.entity_id = ? ");
			sb.append(" union all ");
			sb.append(" select * from oms_oper_list oper where oper.entity_type = 4 and oper.entity_id in ( ");
			sb.append(" select merchant_id from oms_normal_sp_list ob where ob.entity_type = 2 and ob.entity_id = ?  and ob.valid_flag = 1 )");
			sb.append(" )  tbl");
			FIND_USER_BY_OM_BRANCH_TABLE = sb.toString();
		}
	}
	
	/**
	 * 分页查询操作员信息
	 * @param paramsMap 有序参数Map
	 * @param pageNo 页码索引
	 * @param pageSize 每页个数
	 * @param sortName 排序列名
	 * @param sortVal 排序方式 1 正序 其他倒序
	 * @return
	 */
	public List<OmsOperList> pagingFindValidOperEmbedList(
			ListOrderedMap paramsMap, Integer pageNo, Integer pageSize, String sortName,
			Integer sortValue){
		
		if (pageNo!=null && pageNo <= 1) {
			pageNo = 1;
		}
		// 如果参数集没有初始化
		if (paramsMap == null) {
			paramsMap = new ListOrderedMap();
		}
		// 设置有效标志
		paramsMap.put(Constants.Jdbc.VALID_FLAG, 1);
		
		return super.findListByClass(OmsOperList.class, paramsMap, pageNo, pageSize, sortName, sortValue);
	}
	
	/**
	 * 通过entitytype 和 entityid分页查询有效归属操作员列表
	 * @author Administrator
	 * @param entity_type
	 * @param entity_id
	 * @param pageNo 页码索引
	 * @param pageSize 每页个数
	 * @param sortName 排序列名
	 * @param sortVal 排序方式 1 正序 其他倒序
	 * @return
	 */
	public List<OmsOperList> pagingFindValidAttributionOperByEntityTypeAndEntityIdEmbedList(
			Integer entity_type,String entity_id,Integer pageNo, Integer pageSize, 
			String sortName,Integer sortValue){
		
		OmsOperList operList = new OmsOperList();
		
		//设置查询参数
		operList.setEntity_type(entity_type);
		operList.setEntity_id(entity_id);
		operList.setValid_flag(1);
		
		return super.findExample(operList, pageNo, pageSize, sortName, sortValue);
	}
	
	/**
	 * 查询操作员详细包含无效操作员
	 * @author Administrator
	 * @param oper_id
	 * @return
	 */
	public OmsOperList findOperDetailByOperIdIncludeInvalid(String oper_id){
		OmsOperList operList = new OmsOperList();
		//设置operid
		operList.setOper_id(oper_id);
		List<OmsOperList> omsOperLists = super.findExample(operList, null, null, null, null);
		if(omsOperLists!=null && omsOperLists.size()>0){
			return omsOperLists.get(0);
		}
		return null;
	}
	
	/**
	 * 删除操作员信息
	 * @author Administrator
	 * @param oper_id
	 * @return
	 */
	public int logicDeleteOperByOperId(String oper_id){
		
		OmsOperList operList = new OmsOperList();
		//设置operid
		operList.setOper_id(oper_id);
		return super.LogicDeleteObject(operList);
	}
	/**
	 * 
	 * @param entityType
	 * @param entityId
	 * @throws OmsDaoException
	 */
	public void logicDelOperByEntity(int entityType,String entityId)throws OmsDaoException
	{
		int flag =  0;
		try
		{
			flag = this.getJdbcTemplate().update("update oms_oper_list set valid_flag = 0 where entity_type = ? and entity_id = ?", new Object[]{entityType,entityId});
		}
		catch(Exception e)
		{
			throw new OmsDaoException(e.getMessage(),e);
		}
		if (flag < 0)
		{
			throw new OmsDaoException("logic del fail");
		}
	}
	/**
	 * 分业务类型查询用户最大的id
	 * @param entityType
	 * @return
	 * @throws OmsDaoException
	 */
	public String findMaxLogicOperId(Integer entityType)throws OmsDaoException
	{
		String sql = "select max(right(oper_id,13))  as maxId from oms_oper_list where entity_type = ?";
//		String sql = "select max(right(oper_id,13))  as maxId from oms_oper_list ";//暂时不分业务类型了
		try
		{
			return (String) this.getJdbcTemplate().queryForObject(sql, new Object[]{entityType}, String.class);
		}
		catch(Exception e)
		{
			logError(e.getMessage(), e);
			throw new OmsDaoException(e);
		}
	}
	/**
	 * 查询运营商管理员下属的管理员信息
	 * @param operMechantId
	 * @param param
	 * @param oper
	 * @return
	 * @throws OmsDaoException
	 */
	public List<OmsOperList> findOpersByPMPageOrAll(String operMechantId,OmsCommonParam param,OmsOperList oper) throws OmsDaoException
	{
		try
		{
			List<Object> queryParamList = new ArrayList<Object>();
			for (int i=0;i<OmsOperListDao.Sql.OPER_MERCHANT_TABLE_PNUM;i++)
			{
				queryParamList.add(operMechantId);
			}
			return this.pagingFind(param, oper, OmsOperListDao.Sql.FIND_USER_BY_OPER_MERCHANT_TABLE, queryParamList);
		}
		catch(Exception e)
		{
			logError(e.getMessage(), e);
			throw new OmsDaoException(e);
		}
	}
	
	
	/**
	 * 查询运营商管理员下属的管理员总数信息
	 * @param operMechantId
	 * @param param
	 * @param oper
	 * @return
	 * @throws OmsDaoException
	 */
	public int findOpersCountByPM(String operMechantId,OmsCommonParam param,OmsOperList oper) throws OmsDaoException
	{
		try
		{
			List<Object> queryParamList = new ArrayList<Object>();
			for (int i=0;i<OmsOperListDao.Sql.OPER_MERCHANT_TABLE_PNUM;i++)
			{
				queryParamList.add(operMechantId);
			}
			return this.queryTotalByObjectTblName(oper, OmsOperListDao.Sql.FIND_USER_BY_OPER_MERCHANT_TABLE, queryParamList);
		}
		catch(EmptyResultDataAccessException e)
		{
			return 0;
		}
		catch(Exception e)
		{
			logError(e.getMessage(), e);
			throw new OmsDaoException(e);
		}
	}
	
	/**
	 * 查询厂商管理员下属用户
	 * @param factoryId
	 * @param param
	 * @param oper
	 * @return
	 * @throws OmsDaoException
	 */
	public List<OmsOperList> findOpersByFacPageOrAll(String factoryId,OmsCommonParam param,OmsOperList oper) throws OmsDaoException
	{
		try
		{
			List<Object> queryParamList = new ArrayList<Object>();
			for (int i=0;i<OmsOperListDao.Sql.FACTORY_TABLE_PNUM;i++)
			{
				queryParamList.add(factoryId);
			}
			return this.pagingFind(param, oper, OmsOperListDao.Sql.FIND_USER_BY_FACTORY_TABLE, queryParamList);
		}
		catch(Exception e)
		{
			logError(e.getMessage(), e);
			throw new OmsDaoException(e);
		}
	}
	
	/**
	 * 查询厂商管理员下属用户总数
	 * @param factoryId
	 * @param param
	 * @param oper
	 * @return
	 * @throws OmsDaoException
	 */
	public int findOpersCountByFac(String factoryId,OmsCommonParam param,OmsOperList oper) throws OmsDaoException
	{
		try
		{
			List<Object> queryParamList = new ArrayList<Object>();
			for (int i=0;i<OmsOperListDao.Sql.FACTORY_TABLE_PNUM;i++)
			{
				queryParamList.add(factoryId);
			}
			return this.queryTotalByObjectTblName(oper, OmsOperListDao.Sql.FIND_USER_BY_FACTORY_TABLE, queryParamList);
		}
		catch(EmptyResultDataAccessException e)
		{
			return 0;
		}
		catch(Exception e)
		{
			logError(e.getMessage(), e);
			throw new OmsDaoException(e);
		}
	}
	
	/**
	 * 查询运营商分支结构管理员下属用户
	 * @param factoryId
	 * @param param
	 * @param oper
	 * @return
	 * @throws OmsDaoException
	 */
	public List<OmsOperList> findOpersByPMBPageOrAll(String factoryId,OmsCommonParam param,OmsOperList oper) throws OmsDaoException
	{
		try
		{
			List<Object> queryParamList = new ArrayList<Object>();
			for (int i=0;i<OmsOperListDao.Sql.OM_BRANCH_TABLE_PNUM;i++)
			{
				queryParamList.add(factoryId);
			}
			return this.pagingFind(param, oper, OmsOperListDao.Sql.FIND_USER_BY_OM_BRANCH_TABLE, queryParamList);
		}
		catch(Exception e)
		{
			logError(e.getMessage(), e);
			throw new OmsDaoException(e);
		}
	}
	
	/**
	 * 查询运营商分支结构管理员下属用户
	 * @param factoryId
	 * @param param
	 * @param oper
	 * @return
	 * @throws OmsDaoException
	 */
	public int findOpersCountByPMB(String factoryId,OmsCommonParam param,OmsOperList oper) throws OmsDaoException
	{
		try
		{
			List<Object> queryParamList = new ArrayList<Object>();
			for (int i=0;i<OmsOperListDao.Sql.OM_BRANCH_TABLE_PNUM;i++)
			{
				queryParamList.add(factoryId);
			}
			return this.queryTotalByObjectTblName(oper, OmsOperListDao.Sql.FIND_USER_BY_OM_BRANCH_TABLE, queryParamList);
		}
		catch(EmptyResultDataAccessException e)
		{
			return 0;
		}
		catch(Exception e)
		{
			logError(e.getMessage(), e);
			throw new OmsDaoException(e);
		}
	}

	public void logicDelOperStationByOperId(String oper_id)throws OmsDaoException {
		int flag =  0;
		try
		{
			flag = this.getJdbcTemplate().update("update oms_oper_station set valid_flag = 0 where oper_id = ? ", new Object[]{oper_id});
		}
		catch(Exception e)
		{
			throw new OmsDaoException(e.getMessage(),e);
		}
		if (flag < 0)
		{
			throw new OmsDaoException("logic del oper@station fail");
		}
		
	}
	
	
}